package com.yst.metho.web.application.rights;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;

import nextapp.echo2.app.CheckBox;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.FillImage;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Panel;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SplitPane;
import nextapp.echo2.app.TextField;
import nextapp.echo2.extras.app.TabPane;
import nextapp.echo2.extras.app.layout.TabPaneLayoutData;

import com.yst.metho.web.application.components.DoubleListComponent;
import com.yst.metho.web.application.projectversion.ProjectDetailTabbedPane;
import com.yst.metho.web.application.user.renderer.UserListRenderer;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.metho.web.application.utils.Styles;
import com.yst.suivi.data.rights.Profil;
import com.yst.suivi.data.rights.Right;

public class RightsPane extends SplitPane
{
	private static final Extent DEFAULT_MENU_HEIGHT = new Extent(54);

	private TextField mNom;

	private Hashtable mRightsBox = null;

	private DoubleListComponent mTeam;

	private String mId = null;

	public RightsPane(Collection profilUsr, Collection listUsr, Profil profil)
	{
		super(SplitPane.ORIENTATION_VERTICAL, DEFAULT_MENU_HEIGHT);
		setStyleName(Styles.FIXED_SPLIT_PANE_STYLE);
		add(createNamePanel());

		TabPane tabPane = new TabPane();
		for (int index = 0; index < ProjectDetailTabbedPane.TAB_FILL_IMAGES.length; index++)
		{
			setTestComponentProperty(tabPane, TabPane.PROPERTY_TAB_ACTIVE_BACKGROUND_IMAGE, FillImage.class, ProjectDetailTabbedPane.TAB_FILL_IMAGES[index]);
			setTestComponentProperty(tabPane, TabPane.PROPERTY_TAB_INACTIVE_BACKGROUND_IMAGE, FillImage.class, ProjectDetailTabbedPane.TAB_FILL_IMAGES[index]);
		}
		tabPane.setTabPosition(TabPane.TAB_POSITION_BOTTOM);
		TabPaneLayoutData layoutData = new TabPaneLayoutData();
		layoutData.setTitle("Rights");

		add(tabPane);
		Panel pane = createRightPane();
		pane.setLayoutData(layoutData);
		tabPane.add(pane);

		layoutData = new TabPaneLayoutData();
		layoutData.setTitle("Users");
		pane = createUserPane(profilUsr, listUsr);
		pane.setLayoutData(layoutData);
		tabPane.add(pane);

		Right r;
		CheckBox c;
		if (null != profil)
		{
			mId = profil.getId();
			mNom.setText(profil.getNom());
			Enumeration enum1 = profil.getRight();
			while (enum1.hasMoreElements())
			{
				r = (Right) enum1.nextElement();
				c = (CheckBox) mRightsBox.get(r);
				if (null != c)
				{
					c.setSelected(true);
				}
			}
		}
	}

	/**
	 * 
	 */
	private Panel createRightPane()
	{
		Panel retour = new Panel();
		Right r = null;
		Label label;
		CheckBox cb;
		mRightsBox = new Hashtable();
		Grid headerGrid = new Grid();
		headerGrid.setInsets(new Insets(0, 2));
		retour.add(headerGrid);
		for (int i = 0; i < Right.getElements().size(); i++)
		{
			r = (Right) Right.getElements().get(i);
			label = new Label(r.getName());
			headerGrid.add(label);
			cb = new CheckBox();
			headerGrid.add(cb);
			mRightsBox.put(r, cb);
		}
		return retour;
	}

	/**
	 * 
	 */
	private Panel createUserPane(Collection profilUsr, Collection listUsr)
	{
		Panel retour = new Panel();
		mTeam = new DoubleListComponent(profilUsr, listUsr, new UserListRenderer());
		retour.add(mTeam);
		return retour;
	}

	public RightsPane(Collection profilUsr, Collection listUsr)
	{
		this(profilUsr, listUsr, null);
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return mId;
	}

	/**
	 * retourne le nom du profil.
	 * 
	 * @return
	 */
	public String getNom()
	{
		return mNom.getText();
	}

	/**
	 * Retourne la liste des droits selectionnes
	 * 
	 * @return
	 */
	public Collection getRights()
	{
		ArrayList retour = new ArrayList();
		Enumeration enumKeys = mRightsBox.keys();
		Right rKey;
		while (enumKeys.hasMoreElements())
		{
			rKey = (Right) enumKeys.nextElement();
			if (((CheckBox) mRightsBox.get(rKey)).isSelected())
			{
				retour.add(rKey);
			}
		}
		return retour;
	}

	/**
	 * Methode qui permet de construire le panneau d'information des droits.
	 * 
	 * @return
	 */
	private Component createNamePanel()
	{
		Row retour = new Row();
		Label label;
		label = new Label("Nom");
		retour.add(label);
		mNom = new TextField();
		mNom.setStyleName("TextField");
		retour.add(mNom);
		return retour;
	}

	private void setTestComponentProperty(TabPane tabPane, String propertyName, Class propertyClass, Object newValue)
	{
		try
		{
			String methodName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
			Method setPropertyMethod = tabPane.getClass().getMethod(methodName, new Class[]
			{ propertyClass });
			setPropertyMethod.invoke(tabPane, new Object[]
			{ newValue });
		}
		catch (NoSuchMethodException ex)
		{
			ExceptionMgt.manage(ex);
		}
		catch (IllegalArgumentException ex)
		{
			ExceptionMgt.manage(ex);
		}
		catch (IllegalAccessException ex)
		{
			ExceptionMgt.manage(ex);
		}
		catch (InvocationTargetException ex)
		{
			ExceptionMgt.manage(ex);
		}
	}

	/**
	 * Retourne la liste des utilisateurs appartenant aux profils.
	 * 
	 * @return
	 */
	public Collection getUsers()
	{
		return mTeam.getSelectedList();
	}
}
